#include "fun.h"
#include "iic.h"

u8 bdata LED;
u16 time = 0;
sbit L1 = LED^0;
sbit L2 = LED^1;
sbit L3 = LED^2;
u8 i = 0,disp_status = 0,error_count = 1,t_100ms = 1; 
u8 code t_display[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,//0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 
    0x00,0x37,0x73,0x3E};//black   N    P    U  
         
u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //位码

u8 disp_data[8]={0,20,20,20,20,0,0,0};      
u8 Vin_data[8]={23,20,20,20,20,0,0,0};      
u8 Vp_data[8]={22,20,20,20,20,0,0,0};      
u8 count_data[8]={21,0,0,0,0,0,0,0};      

void Timer2Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0x20;		//设置定时初值
	T2H = 0xD1;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
    IE2 |= 0x04;
    EA = 1;
}


void smg_pro(void)
{
    if(disp_status == 0)        //显示采集的电压
        disp_data[i] = Vin_data[i];
    else if(disp_status == 1)       //显示电压参数Vp
        disp_data[i] = Vp_data[i];
    else if(disp_status == 2)       //显示计数值N
    {
        if((count_data[i] == 0)&&(i != 7))
        {
            if((count_data[i-1] != 0)&&(i != 1))
                disp_data[i] = count_data[i];
            else
                disp_data[i] = 20;
        }
        else    
            disp_data[i] = count_data[i];
    }
}

void main(void)
{
    u8 Vp_Temp,Vin_Temp,last_Vp_temp,j,last_status = 0;
    u16 temp;
    L2 = 0;
    buzzer(0);
    relay(0);
    led(LED);

    Vp_Temp = read_24c02(0x00);
    if(Vp_Temp > 50)
    {
        Vp_data[5] = 15;
        Vp_data[6] = 0;
    }
    else 
    {
        Vp_data[5] = (Vp_Temp/10) + 10;
        Vp_data[6] = Vp_Temp%10;
    }
    temp = read_adc();
    temp = temp * 1.961;
    Vin_data[5] = (temp/100) + 10;
    Vin_data[6] = (temp%100)/10;
    Vin_data[7] = temp%10;
    Timer2Init();
        
    while(1)
    {
        if(t_100ms)     //每0.1s采集一次电压数据
        {
            t_100ms = 0;
            temp = read_adc();
            temp = temp * 1.961;
            Vin_data[5] = (temp/100) + 10;
            Vin_data[6] = (temp%100)/10;
            Vin_data[7] = temp%10;
            Vp_Temp = (Vp_data[5] - 10) * 10 + Vp_data[6];
            Vin_Temp = (Vin_data[5] - 10) * 10 + Vin_data[6];

            if(Vin_Temp < Vp_Temp)      //判断计数值+1条件
            {
                if((last_status == 1)&&(last_Vp_temp == Vp_Temp))    //last_status从1到0时计数值加一
                {
                    if(count_data[7] == 9)
                    {
                        count_data[7] = 0;
                        if(count_data[6] == 9)
                        {
                            count_data[6] = 0;
                            if(count_data[5] == 9)
                            {
                                count_data[5] = 0;
                                if(count_data[4] == 9)
                                {
                                    count_data[4] = 0;
                                    if(count_data[3] == 9)
                                    {
                                        count_data[3] = 0;
                                        if(count_data[2] == 9)
                                        {
                                            count_data[2] = 0;
                                            if(count_data[1] == 9)
                                            {
                                                count_data[1] = 0;
                                            }
                                            else
                                                count_data[1] += 1;
                                        }
                                        else
                                            count_data[2] += 1;
                                    }
                                    else
                                        count_data[3] += 1;
                                }
                                else    
                                    count_data[4] += 1;
                            }
                            else
                                count_data[5] += 1;
                        }
                        else
                            count_data[6] += 1;
                    }
                    else
                        count_data[7] += 1;
                    
                    if(count_data[7] % 2)
                        L2 = 1;
                    else
                        L2 = 0;
                    led(LED);
                }  
                last_status = 0;     
            }
            else
                last_status = 1;
            last_Vp_temp = Vp_Temp;
        }

        switch(key_value)
        {
            case(0): break;
            case(12):       //切换显示界面
            {
                key_value = 0;
                disp_status++;
                if(disp_status == 3)
                    disp_status = 0;
                else if(disp_status == 2)     //退出参数设置界面时保存Vp
                {
                    Vp_Temp = (Vp_data[5] - 10) * 10 + Vp_data[6];
                    write_24c02(0x00,Vp_Temp);
                }
                if(L3 == 1)
                {
                    L3 = 0;
                    led(LED);
                }
                error_count = 1;
                break;
            }
            case(13):       //计数值清零，在计数界面有效
            {
                key_value = 0;
                if(disp_status == 2)
                {
                    for(j=1;j<8;j++)
                        count_data[j] = 0;
                }
                if(L3 == 1)
                {
                    L3 = 0;
                    led(LED);
                }
                error_count = 1;
                break;
            }
            case(16):       //Vp加0.5V，参数设置界面有效
            {
                key_value = 0;
                if(disp_status == 1)
                {
                    if(Vp_data[6] == 0)
                    {
                        if(Vp_data[5] == 15)
                        {
                            Vp_data[5] = 10;
                            Vp_data[6] = 0;
                        }
                        else
                            Vp_data[6] = 5;
                    }
                    else
                    {
                        Vp_data[5] += 1;
                        Vp_data[6] = 0;
                    }
                }
                if(L3 == 1)
                {
                    L3 = 0;   
                    led(LED);
                }
                error_count = 1;
                break;
            }
            case(17):       //Vp减0.5V，参数设置界面有效
            {
                key_value = 0;
                if(disp_status == 1)
                {
                    if(Vp_data[6] == 5)
                        Vp_data[6] = 0;
                    else
                    {
                        if(Vp_data[5] > 10)
                        {
                            Vp_data[5] -= 1;
                            Vp_data[6] = 5;
                        }
                        else
                        {
                            Vp_data[5] = 15;
                            Vp_data[6] = 0;
                        }
                    }
                }
                if(L3 == 1)
                {
                    L3 = 0;
                    error_count = 1;
                    led(LED);
                }
                break;
            }
            default: //无效按键
            {
                key_value = 0;
                if(error_count == 3) //连续三次无效按键点亮L3
                {
                    L3 = 1; 
                    led(LED);
                }
                else if(error_count < 3)
                    error_count++;
                break;
            }
        }
    }
}

void t2_callback(void) interrupt 12
{
    static u16 t = 0;
	u8 Vp_temp,Vin_temp;
    t++;
    i++;
    i %= 8;

    Vp_temp = (Vp_data[5] - 10) * 10 + Vp_data[6];
    Vin_temp = (Vin_data[5] - 10) * 10 + Vin_data[6];
    if(Vin_temp < Vp_temp)      //Vp>Vin持续五秒点亮L1
    {
        time++;
        if(time >= 5000)
        {
            time = 0;
            L1 = 1;
            led(LED);
        }
    }
    else
    {
        time = 0;
        if(L1 == 1)
        {
            L1 = 0;
            led(LED);
        }
    }
    
    if(!(t%20))
        key_scan();
    if(!(t%100))
        t_100ms = 1;
    

    if(t == 2000)
        t = 0;
    
    smg_pro();
    smg_disp(t_display[disp_data[i]],T_COM[i]);
}